package kt.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import kt.dal.DBPoolX;
import kt.dal.DBPoolXName;
import kt.smpp.pdu.SMSRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class SmsUtil {

	private static Logger logger = LogManager.getLogger(SmsUtil.class);
	private static String pattern_phone_no = "^(855|0)?(([89][0-9]{8})|([0-9]{2}6[0-9]{6}))$";
	public static String PATTERN_SEPARATOR = "[,\\. ;:+/\\{\\}\\^\\&\\(\\)\"\\?\\<\\>\\~\\`\\!\\@\\#\\$\\%\\-=\\+']";

	public static boolean isValidMobileNumber(String mobileNumber) {
		if ((mobileNumber == null) || ("".equals(mobileNumber))) {
			return false;
		}
		mobileNumber = processIsdn(mobileNumber);
		if (mobileNumber.length() != 12) {
			return false;
		}
		Pattern p = Pattern.compile(pattern_phone_no);
		Matcher m = p.matcher(mobileNumber);
		return m.find();
		// return mobileNumber.length() > 9 && mobileNumber.length() < 15;
	}

	public static boolean isValidEmail(String email) {
		if ((email == null) || ("".equals(email))) {
			return false;
		}

		return email.indexOf("@") > 2;
	}
	
	public static void Rollback(Connection cnn){
		try {
			cnn.rollback();
		}
		catch (Exception e){
			logger.error(e.getMessage());
		}
	}

	public static List<String> split(String str) {
		String[] strs = str.split(PATTERN_SEPARATOR);
		List<String> infos = new ArrayList<String>();
		for (String s : strs) {
			if (s.trim().length() > 0) {
				infos.add(s.trim());
			}
		}
		return infos;
	}

	public static String processIsdn(String isdn) {
		String re = "";
		re = isdn.replaceAll(PATTERN_SEPARATOR, "");
		re = re.trim();
		if (re.length() > 3 && "0".equals(re.substring(0, 1))) {
			re = "855" + re.subSequence(1, re.length());
		}
		if (!re.startsWith("855")) {
			re = "855" + re;
		}
		return re;
	}

	public static void revertInfo(SMSRequest request) {
		if (request.isTangBanBe()) {
			request.setInfo(request.getInfo() + " "
					+ request.getReceiverNumber());
			request.setReceiverNumber(request.getSenderNumber());
		}
	}

	
	public static void sendSms(String isdn, String content, String serviceNo, String mobileOperator) {
		DBPoolX smsPool = null;

		Connection conn = null;
		PreparedStatement preStmt = null;
		String strSQL = null;
		// *
		try {
			smsPool = DBPoolX.getInstance(DBPoolXName.SMS_DEFAULT);
			conn = smsPool.getConnection();

			strSQL = "INSERT INTO SMS_SEND_QUEUE ( SENDER_NUMBER, RECEIVER_NUMBER, SERVICE_NUMBER, MOBILE_OPERATOR, COMMAND_CODE, CONTENT_TYPE, MESSAGE_TYPE, INFO, GEN_DATE, REQUEST_ID) "
					+ " VALUES (	 					?, 			?, 				?, 				?, 				'INV', 			0, 			0, 				?, 		now(), 0)";

			preStmt = conn.prepareStatement(strSQL);
			preStmt.setString(1, isdn);
			preStmt.setString(2, isdn);
			preStmt.setString(3, serviceNo);
			preStmt.setString(4, mobileOperator);
			preStmt.setString(5, content);
			preStmt.executeUpdate();
		} catch (Exception e) {
			logger.error("sendSms to admin: Error executing SQL " + strSQL + ">>>" + e.toString());
		} finally {
			if (smsPool != null) {
				smsPool.releaseConnection(conn, preStmt);
			}
		}
	}
	
	public static boolean isInteger(String strInteger) {
		try{
			Integer.parseInt(strInteger);
			return true;
		} catch(NumberFormatException nFE) {
			return false;
		}
	}
	
	public static boolean isCellCardNumber(String number){
		String[] cellcard= {"85512","85517","85577", "85578", "85589", "85592", "85595"}; 
		String sub = number.substring(0, 5);
		for (String s : cellcard) {
			if(sub.equals(s)){
				return true;
			}
		}
		
		return false;
	}
	
	public static boolean isMetfoneNumber(String number){
		String[] cellcard= {"85597", "85588"}; 
		String sub = number.substring(0, 5);
		for (String s : cellcard) {
			if(sub.equals(s)){
				return true;
			}
		}
		
		return false;
	
	}
	public static boolean isOthersNumber(String number){
		String[] cellcard= {"85511", "85561", "85576", "85585", "85599","85510", "85569", "85570", "85586", "85593", "85596", "85598","85515", "85516", "85581", "85587"}; 
		String sub = number.substring(0, 5);
		for (String s : cellcard) {
			if(sub.equals(s)){
				return true;
			}
		}
		
		return false;
	}
}
